package org.rc.web;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.rc.model.Result;

/**
 * Model for a jquery datatable for displaying results
 * @author dokeeffe
 */
public class ResultsTablePage extends JqueryDataTablePage<Result> {

    public ResultsTablePage(List<Result> data) {
        loadTable(data);
    }
    
    public void loadTable(List<Result> data) {
        this.setAaData(new ArrayList<List<String>>()); 
        for(Result r : data) {
            List<String> row = new ArrayList<String>();
            row.add(buildAthleteLink(r));
            row.add(""+r.getPlace());
            row.add(r.getCategory());
            row.add(r.getClub());
            DateFormat df = new SimpleDateFormat("HH':'mm':'ss");
            row.add(df.format(new Date(r.getTimeInSeconds()*1000)));
            getAaData().add(row);
        }
    }

    private String buildAthleteLink(Result r) {
        return "<a href =\"athlete?id="+r.getAthlete().getId()+"\">" + r.getRecordedAthleteName() + "</a>";
    }

}
